package com.nbsaas.boot.ai.ext.resource;

import com.nbsaas.boot.ai.api.apis.AiSessionApi;
import com.nbsaas.boot.ai.api.domain.field.AiSessionField;
import com.nbsaas.boot.ai.api.domain.request.AiSessionRequest;
import com.nbsaas.boot.ai.api.domain.response.AiSessionResponse;
import com.nbsaas.boot.ai.ext.apis.AiSessionExtApi;
import com.nbsaas.boot.rest.filter.Filter;
import com.nbsaas.boot.rest.response.ResponseObject;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class AiSessionExtResource implements AiSessionExtApi {

    @Resource
    private AiSessionApi aiSessionApi;

    @Transactional
    @Override
    public ResponseObject<AiSessionResponse> create(AiSessionRequest request) {
        Long num = aiSessionApi.countData(Filter.eq(AiSessionField.sessionKey, request.getSessionKey()));
        if (num == null) {
            num = 0L;
        }
        if (num > 0) {
            return ResponseObject.error("会话已存在");
        }
        return aiSessionApi.create(request);
    }
}
